import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
  OneToMany,
  BeforeInsert,
  BeforeUpdate,
} from 'typeorm';
import { Tournament } from 'src/modules/tournament/entities/tournament.entity';
import { Event } from 'src/modules/event/entities/event.entity';
import { CategoryStatusEnum } from 'src/common/constant/enums';

@Entity('category')
export class Category {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  alias: string;

  @Column()
  minAge: number;

  @Column()
  maxAge: number;

  @Column()
  createBy: number;

  @Column()
  createTime: Date;

  @Column()
  updateBy: number;

  @Column()
  updateTime: Date;

  @ManyToOne(() => Tournament, (tournament) => tournament.categories)
  @JoinColumn({ name: 'fk_tournament_id' })
  tournament: Tournament;

  @Column({ name: 'fk_tournament_id' })
  tournamentId: number;

  @OneToMany(() => Event, (event) => event.category, {
    cascade: true,
  })
  events: Event[];

  @BeforeInsert()
  BeforeInsert() {
    this.createTime = new Date();
    this.updateTime = this.createTime;
  }

  @BeforeUpdate()
  BeforeUpdate() {
    this.updateTime = new Date();
  }

  @Column()
  status: CategoryStatusEnum;
}
